//
//  Root1ViewController.m
//  UI_豆瓣项目
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Root1ViewController.h"
#import "MyView.h"
#import "Root1Model.h"
#import "UIImageView+WebCache.h"
#import "FoutViewController.h"
#import "Fout1ViewController.h"
#import <sqlite3.h>
#import "DataBaseHandle.h"
@interface Root1ViewController ()
@property (nonatomic, retain)NSMutableArray *arrModel;
@property (nonatomic, assign)NSInteger pp;
@property (nonatomic, retain)NSArray *arr;
@end

@implementation Root1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
    [self.view addSubview:scroll];
    scroll.contentSize = CGSizeMake(0, 667 * 3);
    // 收藏
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btn_nav_share@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    
    //xib
    MyView *view_ = [[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] lastObject];
    
    [scroll addSubview:view_];
    // JSON
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSLog(@"%@", data);
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"%@", result);
    NSArray *arr = [result objectForKey:@"events"];
    self.arrModel = [NSMutableArray array];
    for (NSMutableDictionary *dic in arr) {
        Root1Model *model = [Root1Model root1Model];
        [model setValuesForKeysWithDictionary:dic];
        [_arrModel addObject:model];
    }
//    属性传值
    Root1Model *model = _arrModel[_index];
    view_.neirongLabel.text = model.content;
    view_.neirongLabel.numberOfLines = 1000;
    view_.bigLabel.text = _big;
    view_.timeLabel.text = _time;
    view_.addLabel.text = _add;
    view_.leiXingLabel.text = _leiXing;
    view_.didianLabel.text = model.name;
    //网络图片
    [view_.imge sd_setImageWithURL:[NSURL URLWithString:self.img]];
}
- (void)itemAction:(UIBarButtonItem *)item {
//    FoutViewController *vc = [[FoutViewController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
   Root1Model *model = _arrModel[_index];
    NSLog(@"%@", model.title);
    [[DataBaseHandle sharedDataBaseHandle] createTable];
    [[DataBaseHandle sharedDataBaseHandle] openDB];
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否收藏" preferredStyle:(UIAlertControllerStyleAlert)];
    
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        NSMutableArray *arr = [[DataBaseHandle sharedDataBaseHandle] selectTable];
        
        if (![arr containsObject:model.title]) {
//            [[DataBaseHandle sharedDataBaseHandle] insertModel:model];
        } else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已添加" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction:alertAction];
            [alert addAction:alertAction1];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }

        }];
    UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"收藏失败");
    }];
    [alert addAction:alertAction];
    [alert addAction:alertAction1];
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    

    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
